﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Threading;
using System.Data;
using System.Data.SqlClient;

namespace QLDiemDHBKServer
{
       
    public partial class Form1 : Form
    {
        private Socket sock = null;
        protected SqlConnection con;
        protected SqlCommand cmd;
        protected SqlDataReader sda;
        protected String cn;

        public Form1()
        {
            InitializeComponent();
        }

        private void connect()
        {
            string cn = "Data Source=192.168.1.102;Initial Catalog=QLDiem;Integrated Security=True";

            try
            {
                con = new SqlConnection(cn);
                con.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Khong the ket noi co so du lieu", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnstartserver_Click(object sender, EventArgs e)
        {
            btnstartserver.Text = "In process...";
            Application.DoEvents();

            new Thread(new ThreadStart(processing)).Start();
        }
        private void processing()
        {
            IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 5656);
            sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            sock.Bind(ipEnd);
            sock.Listen(100);
            byte[] receive = new byte[1024];
            while (true)
            {
                Socket clientSock = sock.Accept();
                DataTable table = (DataTable)thuda();
                //DataSet dstest = new DataSet();
                //DataSet dstest = new QLDiemDataSet();
                //clientSock.Receive(receive);
                //string hivongdung = (string)DeserializeData(receive);
                //MessageBox.Show(hivongdung);
                clientSock.Send(SerializeData(table));
            }
        }

        public object DeserializeData(byte[] theByteArray)
        {
            MemoryStream ms = new MemoryStream(theByteArray);
            BinaryFormatter bf1 = new BinaryFormatter();
            ms.Position = 0;
            return bf1.Deserialize(ms);
        }

        public byte[] SerializeData(Object o)
        {
            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf1 = new BinaryFormatter();
            bf1.Serialize(ms, o);
            return ms.ToArray();
        }

        internal object thuda()
        {
            DataTable dt = new DataTable();
            connect();
            try
            {
                string sql = "Select * from Diem";
                cmd = new SqlCommand(sql, con);
                cmd.CommandType = CommandType.Text;
                sda = cmd.ExecuteReader();
                dt.Load(sda);
            }
            catch (Exception)
            {
                return dt;
            }
            finally
            {
                con.Close();
            }
            return dt;
        }
    }
}
